<?php
$secretKey = "app_test";

$uid = $_GET["uid"];
$sig = $_GET["sig"];
$sessionkey = $_GET["sessionkey"];
if ($sig != md5($sessionkey . $uid . $secretKey)) {
    die("sig invalid");
}

$webRoot = "http://localhost/xsocial/";
?>
<script type="text/javascript" src="<?php echo $webRoot ?>js/xsocialClient.js"></script>
<h1>This is a sample app.</h1>

<button onclick="post()">Post Feed</button>
<button onclick="invite()">Invite</button>

<div id="user">
<div id="id"></div>
<div id="name"></div>
<img id="thumbnail" src="" width="100px" height="100px">
<div id="userData"></div>
</div>
<div id="friends"></div>
<script type="text/javascript">
//xsocial.getViewer(showViewer);
//xsocial.getFriends(showFriends);
xsocial.getAll(function(data) {
    showViewer(data.viewer); 
    showFriends(data.friends);
    xsocial.adjustHeight();
});

function showViewer(viewer) {
    document.getElementById("id").innerHTML = viewer.uid;
    document.getElementById("name").innerHTML = viewer.name;
    document.getElementById("userData").innerHTML = JSON.stringify(viewer);
    document.getElementById("thumbnail").src = viewer.thumbnail;
}

function showFriends(friends) {
    var html = "";
    for(var i=0;i < friends.length; i++) {
        var friend = friends[i];
        html += "uid:" + friend["uid"] + "<br>";
        html += "name:" + friend["name"] + "<br>";
        html += "thumbnail:" + "<img src='" + friend["thumbnail"] + "' ><br>";
    }
    document.getElementById("friends").innerHTML = html;
}

function post() {
    xsocial.post({
        'title':'Feed from XSocial',
        'content':'Welcome to XSocial!',
        'pic':'http://code.google.com/p/xsocial/logo',
        'link':'http://xsocial.googlecode.com'
        },
        function(result) {
            if (result.error) {
                alert(result.message);
            } else {
                alert('message sent');
            }
        }
    );
}

function invite() {
    xsocial.invite({"title":"Welcome to XSocial","content":"try XSocial!"},function(result)
    {
        if (result.error) {
                alert(result.message);
            } else {
                alert("you have invite friends:" + JSON.stringify(result));
            }
    });
}
</script>